#!/bin/sh

# Start/stop/restart wildfly.

# Copyright 2017 Giorgio Peron <giorgio.peron@gmail.com>, Belluno, Italy
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# define where wildfly is - this is the directory containing directories log, bin, conf etc
WILDFLY_HOME=${WILDFLY_HOME:-"/usr/share/wildfly"}

# make java is on your path
JAVAPTH=${JAVAPTH:-"$JAVA_HOME/bin"}

# define the user under which wildfly will run, or use RUNASIS to run as the current user
WILDFLYUS=${WILDFLYUS:-"wildfly"}

export LAUNCH_WILDFLY_IN_BACKGROUND=yes

# define the script to use to start standalone wildfly
WILDFLY_START_STANDALONE=${WILDFLY_START_STANDALONE:-"$WILDFLY_HOME/bin/standalone.sh "}

# define the script to use to shutdown wildfly
# change host and port as need
WILDFLY_STOP_STANDALONE=${WILDFLY_STOP_STANDALONE:-"$WILDFLY_HOME/bin/jboss-cli.sh --connect controller=127.0.0.1:9990 command=:shutdown"}

# define log file
WILDFLY_CONSOLE="/var/log/wildfly/wildfly.log"
WILDFLY_DOMAIN_CONSOLE="/var/log/wildfly/wildfly_domain.log"

if [ -n "$WILDFLY_CONSOLE" -a ! -d "$WILDFLY_CONSOLE" ]; then
  # ensure the file exists
  touch $WILDFLY_CONSOLE
  if [ "$WILDFLYUS" != "RUNASIS" ]; then
    chown -R $WILDFLYUS.$WILDFLYUS $WILDFLY_CONSOLE
  fi
fi

if [ -n "$WILDFLY_CONSOLE" -a ! -f "$WILDFLY_CONSOLE" ]; then
  echo "WARNING: location for saving console log invalid: $WILDFLY_CONSOLE"
  echo "WARNING: ignoring it and using /dev/null"
  WILDFLY_CONSOLE="/dev/null"
fi

# define what will be done with the console log
WILDFLY_CONSOLE=${WILDFLY_CONSOLE:-"/dev/null"}

if [ -n "$WILDFLY_DOMAIN_CONSOLE" -a ! -d "$WILDFLY_DOMAIN_CONSOLE" ]; then
  # ensure the file exists
  touch $WILDFLY_DOMAIN_CONSOLE
  if [ "$WILDFLYUS" != "RUNASIS" ]; then
    chown -R $WILDFLYUS.$WILDFLYUS $WILDFLY_DOMAIN_CONSOLE
  fi
fi

if [ -n "$WILDFLY_DOMAIN_CONSOLE" -a ! -f "$WILDFLY_DOMAIN_CONSOLE" ]; then
  echo "WARNING: location for saving console log invalid: $WILDFLY_DOMAIN_CONSOLE"
  echo "WARNING: ignoring it and using /dev/null"
  WILDFLY_DOMAIN_CONSOLE="/dev/null"
fi

# define what will be done with the console log
WILDFLY_DOMAIN_CONSOLE=${WILDFLY_DOMAIN_CONSOLE:-"/dev/null"}

CMD_START_STANDALONE="cd $WILDFLY_HOME/bin; $WILDFLY_START_STANDALONE" 
CMD_STOP_STANDALONE="cd $WILDFLY_HOME/bin; $WILDFLY_STOP_STANDALONE"

if [ "$WILDFLYUS" = "RUNASIS" ]; then
  SUBIT=""
else
  SUBIT="su $WILDFLYUS -c "
fi

if [ -z "`echo $PATH | grep $JAVAPTH`" ]; then
  export PATH=$PATH:$JAVAPTH
fi

if [ ! -d "$WILDFLY_HOME" ]; then
  echo "WILDFLY_HOME does not exist as a valid directory : $WILDFLY_HOME"
  exit 1
fi

case "$1" in
start)
	echo "Starting WildFly Application Server"
    cd $WILDFLY_HOME/bin
    if [ -z "$SUBIT" ]; then
        eval $CMD_START_STANDALONE >${WILDFLY_CONSOLE} 2>&1 &
    else
        $SUBIT "$CMD_START_STANDALONE >${WILDFLY_CONSOLE} 2>&1 &" 
    fi
    ;;
stop)
	echo "Stopping WildFly Application Server"
    if [ -z "$SUBIT" ]; then
        $CMD_STOP_STANDALONE >/dev/null 2>&1
    else
        $SUBIT "$CMD_STOP_STANDALONE >/dev/null 2>&1"
    fi 
    ;;
restart)
    $0 stop
    $0 start
    ;;
domain-start)
	echo "Starting Domain WildFly Application Server"
    cd $WILDFLY_HOME/bin
    if [ -z "$SUBIT" ]; then
        eval $CMD_START_DOMAIN >${WILDFLY_DOMAIN_CONSOLE} 2>&1 &
    else
        $SUBIT "$CMD_START_DOMAIN >${WILDFLY_DOMAIN_CONSOLE} 2>&1 &" 
    fi
    ;;
domain-stop)
	echo "Stopping Domain WildFly Application Server"
    if [ -z "$SUBIT" ]; then
        $CMD_STOP_DOMAIN >/dev/null 2>&1
    else
        $SUBIT "$CMD_STOP_DOMAIN >/dev/null 2>&1"
    fi 
    ;;
domain-restart)
    $0 domain-stop
    $0 domain-start
    ;;
*)
    echo "usage: $0 (start|stop|restart|help|domain-start|domain-stop|domain-restart)"
esac
